package com.lanyou.esb.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.SAXException;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.entity.IfAlarmHis;
import com.lanyou.esb.web.param.InterfaceWarningParam;
import com.lanyou.esb.web.serviceMonitor.InterfaceWarningHisController;
import com.lanyou.cook.common.Constants;

/**
 * 导出Excel文件控制层
 * 
 * @author 杨晨睿
 *
 */
@Controller
@RequestMapping(Constants.BASE_URL_API + "/exportDatas")
public class EsbExportDataToExcelFileController {
	
	@Autowired
	private ResourceLoader resourceLoader;
	@Autowired
	private InterfaceWarningHisController interfaceWarningHisController;
	/**
	 * 服务告警明细导出
	 * 
	 * @param param
	 *            类型:String 待导出数据的查询条件
	 * @param request
	 *            类型:HttpServletRequest 客户端请求
	 * @param response
	 *            类型:HttpServletResponse 服务器的响应
	 */
	@ResponseBody
	@RequestMapping(value = "/file/interfaceWarningHisExport", produces = "application/json")
	public void interfaceWarningHisExport(String param, HttpServletRequest request,
			HttpServletResponse response) throws IOException, InvalidFormatException, SAXException {

		JsonObject paramJsonObject = new JsonParser().parse(param).getAsJsonObject();
		InterfaceWarningParam interfaceWarningParam = new Gson().fromJson(paramJsonObject,
				InterfaceWarningParam.class);
		Sort sort = new Sort(Direction.DESC, "createTime");
		Pageable pageable = new PageRequest(Constants.DEFAULT_EXPORT_PAGE_COUNT,
				Constants.DEFAULT_EXPORT_DATA_COUNT,sort);

		Page<IfAlarmHis> pInterfaceWarningHis = interfaceWarningHisController
				.findByExample(interfaceWarningParam, pageable);
		List<IfAlarmHis> interfaceWarningHis = pInterfaceWarningHis.getContent();
		String fileName = new String( Constants.FIELD_INTERFACE_HISTORY_INFORMATION.getBytes("gb2312"), "ISO8859-1" );// 文件名
		InputStream in = resourceLoader.getResource("classpath:jxls/templatestIn/interfaceWarningHis.xls")
				.getInputStream();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		OutputStream os = response.getOutputStream();
		Context context = new Context();
		context.putVar("interfaceWarningHis", interfaceWarningHis);
		JxlsHelper.getInstance().processTemplate(in, os, context);
	}

}
